# vim:ts=4:sw=4:noet:
""" IR optimiser base class """

import c.compiler.ir as ir
from c.base.support import CoreException

class IRVisitor:
	def doMethod(self, meth):
		self.doIRBody(meth.body)

	def doIRNode(self, node):
		if isinstance(node, ir.IRReturn):      return self.doIRReturn(node)
		elif isinstance(node, ir.IRMessage):   return self.doIRMessage(node)
		elif isinstance(node, ir.IRPrimitive): return self.doIRPrimitive(node)
		elif isinstance(node, ir.IRName):      return self.doIRName(node)
		elif isinstance(node, ir.IRAssign):    return self.doIRAssign(node)
		elif isinstance(node, ir.IRInteger):   return self.doIRInteger(node)
		elif isinstance(node, ir.IRString):    return self.doIRString(node)
		elif isinstance(node, ir.IRSymbol):    return self.doIRSymbol(node)
		elif isinstance(node, ir.IRChar):      return self.doIRChar(node)
		elif isinstance(node, ir.IRCascade):   return self.doIRCascade(node)
		elif isinstance(node, ir.IRBlock):     return self.doIRBlock(node)
		elif isinstance(node, ir.IRArray):     return self.doIRArray(node)
		else:
			raise CoreException("Unhandled node %s" %  node)
	
	def doIRBody(self, node):
		for i, st in enumerate(node.statements):
			r,v = self.doIRNode(st)
			if r:
				node.statements[i] = v
		return (None, None)

	def doIRBlock(self, node):
		return self.doIRBody(node)

